#include "BucketsMapper.h"
#include "../DataSourceInstance.h"
#include "../common/YXMySQL.h"
#include "../common/YXString.h"

MYSQLResult * insertBucket(char* bucketName, int accessPolicy, char* createdTime) {
    static char *sqlTemplate = "insert into buckets(name, access_policy, created_time) values('%s', %d, '%s')";
    char * sql = yxString_printf(sqlTemplate, bucketName, accessPolicy, createdTime);

    Connection *connection = getConnection(dataSource);
    MYSQLResult* result = exceuteQuery(connection, sql);
    returnConnection(dataSource, connection);

    return result;
}

MYSQLResult * selectBuckets() {
    static char *sql = "select * from buckets";

    Connection *connection = getConnection(dataSource);
    MYSQLResult* result = exceuteQuery(connection, sql);
    returnConnection(dataSource, connection);

    return result;
}

MYSQLResult* selectBucketSummary(char* bucketName) {
    static char *sqlTemplate = "select * from buckets where name = '%s'";
    char * sql = yxString_printf(sqlTemplate, bucketName);

    Connection *connection = getConnection(dataSource);
    MYSQLResult* result = exceuteQuery(connection, sql);
    returnConnection(dataSource, connection);

    return result;
}